//
//  ViewController.m
//  SoundPlayer
//
//  Created by student on 16/4/5.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
{
    SystemSoundID soundID;
}
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UISlider *Slider;
@property (nonatomic,strong)AVAudioPlayer *player;
@property (weak, nonatomic) IBOutlet UILabel *label1;
@property (nonatomic,strong)AVAudioPlayer *player2;
@property (nonatomic,strong)AVAudioRecorder *recorder2;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)b1:(id)sender
{
    if(soundID==0)
    {
        NSURL *fileurl=[[NSBundle mainBundle]URLForResource:@"AlertChordStroke" withExtension:@"wav"];
        AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(fileurl), &soundID);
    }
    AudioServicesPlaySystemSound(soundID);
}
- (IBAction)b2:(id)sender
{
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
- (IBAction)b3:(id)sender
{
    NSURL *fileurl=[[NSBundle mainBundle]URLForResource:@"charleston1925_64kb" withExtension:@"mp3"];
    self.player=[[AVAudioPlayer alloc] initWithContentsOfURL:fileurl error:nil];
    self.Slider.minimumValue=0;
    self.Slider.maximumValue=self.player.duration;
    [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(updateinfo) userInfo:nil repeats:YES];
    [self.player play];
}
- (IBAction)b4:(id)sender
{
    [self.player stop];
}
- (IBAction)hua:(id)sender
{
    self.player.currentTime=self.Slider.value;
}
-(void)updateinfo
{
    self.Slider.value=self.player.currentTime;
    self.label.text=[NSString stringWithFormat:@"%.1f/%.1f",self.player.currentTime,self.player.duration];
}
-(NSURL *)fileurl
{
    NSString *docpath=[NSHomeDirectory() stringByAppendingString:@"/Documents"];
    NSString *filepath=[docpath stringByAppendingString:@"/1.caf"];
    NSURL *fileurl=[NSURL URLWithString:filepath];
    return fileurl;
}
- (IBAction)luzhi:(id)sender
{
    if(self.recorder2.recording)
    {
        return;
    }
    if(self.player2.playing)
    {
        return;
    }
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
    NSMutableDictionary *settings = [NSMutableDictionary dictionary];
    [settings setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM]
                forKey:AVFormatIDKey];
    [settings setValue:[NSNumber numberWithFloat:44100.0]
                forKey:AVSampleRateKey]; //采样率
    [settings setValue:[NSNumber numberWithInt:1]
                forKey:AVNumberOfChannelsKey];//通道的数目
    [settings setValue:[NSNumber numberWithInt:16]
                forKey:AVLinearPCMBitDepthKey];//采样位数  默认 16
    [settings setValue:[NSNumber numberWithBool:NO]
                forKey:AVLinearPCMIsBigEndianKey];//大端还是小端 是内存的组织方式
    [settings setValue:[NSNumber numberWithBool:NO]
                forKey:AVLinearPCMIsFloatKey];//采样信号是整数还是浮点数
    self.recorder2=[[AVAudioRecorder alloc]initWithURL:[self fileurl] settings:settings error:nil];
    [self.recorder2 record];
    self.label1.text=@"...";
}
- (IBAction)tingzhi:(id)sender
{
    if(self.recorder2.recording)
    {
        [self.recorder2 stop];
    }
    if(self.player2.playing)
    {
        [self.recorder2 stop];
    }
    self.label1.text=@"XXX";
}
- (IBAction)bofang:(id)sender
{
    if(self.recorder2.recording)
    {
        return;
    }
    if(self.player2.playing)
    {
        return;
    }
    self.player2=[[AVAudioPlayer alloc]initWithContentsOfURL:[self fileurl] error:nil];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
    
    [self.player2 play];
    self.label1.text=@"...";

}
@end
